
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="DOM,基础,">
  

  
    <meta name="description" content="DOM操作">
  
  
  
  <link rel="icon" type="image/x-icon" href="/images/footer-logo.png">
  
  <title>DOM操作 [ 51AIOps 专注于运维自动化  微信： kaipython ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    
    <span class="title" style="text-transform:none">51AIOps 专注于运维自动化  微信： kaipython</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            
              <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
            
          
      
  </ul>
   
</nav>

  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner" style='margin-left:-68px!important'>
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        DOM操作
      </h1>
      <span>
        
        <time class="time" datetime="2017-05-07T11:55:09.000Z">
        2017-05-07
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/DOM/">DOM</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/基础/">基础</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <h2 id="DOM操作"><a href="#DOM操作" class="headerlink" title="DOM操作"></a>DOM操作</h2><h3 id="一、前戏"><a href="#一、前戏" class="headerlink" title="一、前戏"></a><div id="title">一、前戏</div></h3><p><strong>截止到目前为止，我们已经学习了javascript的一些简单的语法，但是使用这些简单的语法，并不能做出一些我们经常看到的一些效果，比如说表格的全选和反选等，因此为了能够操作网页上的元素，我们需要继续学习DOM相关的知识。</strong></p>
<p><strong>所谓的DOM，就是使用JS操作整个浏览器，但是这个浏览器对象比较大，因此为了方便操作，又分为三个子对象：</strong></p>
<p>####1. BOM对象</p>
<blockquote>
<p>BOM对象主要操作地址栏，浏览历史，窗口的高度等</p>
</blockquote>
<p>####2. DOM对象</p>
<blockquote>
<p>DOM对象主要操作网页上的元素和相关的CSS</p>
</blockquote>
<p>####3. window对象</p>
<blockquote>
<p>window对象是浏览器的宿主对象，其提供的方法和属性与js语言没有任何关系</p>
</blockquote>
<p><img src="http://i.imgur.com/alowdg5.png" alt></p>
<p><strong>上面的三个对象中，我们主要学习的是DOM对象，通过DOM编程，可以轻易的实现我们常见的一些效果</strong></p>
<hr>
<h3 id="二、DOM对象–查找元素"><a href="#二、DOM对象–查找元素" class="headerlink" title="二、DOM对象–查找元素"></a><div id="title">二、DOM对象–查找元素</div></h3><p>####1.直接查找</p>
<blockquote>
<p>document.getElementById 　　　　　　　根据ID获取一个标签<br>document.getElementsByName　　　　　　根据name属性获取标签集合<br>document.getElementsByClassName　　　根据class属性获取标签集合<br>document.getElementsByTagName　　　　根据标签名获取标签集合</p>
</blockquote>
<p>####2.间接查找</p>
<blockquote>
<p>parentNode　　　　　　    父节点<br>childNodes　　　　　　　所有子节点<br>firstChild 　　　　　　 第一个子节点<br>lastChild 　　　　　　　最后一个子节点<br>nextSibling　　　　　　 下一个兄弟节点<br>previousSibling　　　　上一个兄弟节点</p>
<p>parentElement 　　　　　父节点标签元素<br>children  　　　　　　　　所有子标签<br>firstElementChild　　　第一个子标签元素<br>lastElementChild 　　　最后一个子标签元素<br>nextElementtSibling 　下一个兄弟标签元素<br>previousElementSibling 上一个兄弟标签元素</p>
</blockquote>
<p><strong>案例：表格的全选和反选以及取消</strong></p>
<hr>
<h3 id="三、DOM对象–属性操作"><a href="#三、DOM对象–属性操作" class="headerlink" title="三、DOM对象–属性操作"></a><div id="title">三、DOM对象–属性操作</div></h3><p>####1.操作内容</p>
<blockquote>
<p>innerText　　　文本<br>innerHTML　　　HTML内容<br>value 　　　　　值</p>
</blockquote>
<p>####2.操作属性</p>
<blockquote>
<p>attributes 　　　　　　　　　　　获取所有标签属性<br>setAttribute(key,value) 　　　设置标签属性<br>getAttribute(key)　　　　　　　获取指定标签属性<br>removeAttribute(key)　　　　　删除属性</p>
</blockquote>
<p><strong>案例：验证码倒计时发送</strong></p>
<p>####3.样式操作</p>
<ul>
<li><p><strong>3.1 指定类操作</strong></p>
<blockquote>
<p>className　　　　　　　　　　获取所有类名<br>classList.remove(cls) 　　删除指定类<br>classList.add(cls) 　　　　添加类<br>案例：左侧菜单显示 和 模态框显示取消</p>
</blockquote>
</li>
<li><p><strong>3.2 指定css操作</strong></p>
<blockquote>
<p>obj.style.backgroundColor = “red”;</p>
</blockquote>
</li>
</ul>
<p><img src="http://i.imgur.com/sksySIc.png" alt></p>
<p><strong>使用js操作css的属性的规律如下：</strong></p>
<p>1.对于没有中划线的css属性一般直接使用style.属性名即可。如：</p>
<blockquote>
<p>obj.style.margin，obj.style.width，obj.style.left，obj.style.position等</p>
</blockquote>
<p>2.对于含有中划线的css属性，将每个中划线去掉并将每个中划线后的第一个字符换成大写即可。如：</p>
<blockquote>
<p>obj.style.marginTop，obj.style.borderLeftWidth，obj.style.zIndex，obj.style.fontFamily等</p>
</blockquote>
<p><strong>案例：表格的隔行换色 和 文本框获取失去焦点 以及 小说阅读网站，点击大中小变换字体</strong></p>
<h3 id="四、事件"><a href="#四、事件" class="headerlink" title="四、事件"></a><div id="title">四、事件</div></h3><p>####1.常见的一些事件</p>
<blockquote>
<p>获得焦点事件——–onfocus<br>失去焦点事件——–onblur<br>内容改变事件——–onchange<br>载入页面———–onload<br>单击事件———–onclick<br>鼠标移入事件——–onmouseover<br>鼠标移出事件——–onmouseout</p>
</blockquote>
<p><strong>案例：onchange 多级菜单联动</strong></p>
<p>####2.绑定事件的三种方式</p>
<blockquote>
<p>面试题：写一个行为，样式，结构相分离的页面？    </p>
</blockquote>
<ul>
<li>2.1 html属性的方式绑定</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;a href=<span class="string">'www.baidu.com'</span> onclick=<span class="string">"t1();"</span>&gt; 百度 &lt;<span class="regexp">/a&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>作为WEB开发人员,这是最早碰到的一种事件绑定方式，这种绑定方式非常古老</p>
<p>优点：兼容性最强</p>
<p>缺点也很明显:<br>1:需要在DOM渲染时分析绑定语句,影响DOM渲染速度(IE下尤其)<br>2:形式上没达到”结构与行为”相分离的原则<br>3:t函数在全局内执行,不方便访问该DOM对象</p>
</blockquote>
<ul>
<li>2.2 对象属性方式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> bd = <span class="built_in">document</span>.getElementById(<span class="string">'bd'</span>);</span><br><span class="line">bd.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="keyword">this</span>.id);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>这种绑定是把一个DOM对象onclick属性赋值为一个函数，那此时函数内部的this指向的是该DOM对象，即这个a对象</p>
<p>缺点:该方式只能为一种事件，绑定一个回调函数</p>
<p>即：.onclick = fn1, .onclick=fn2;<br>最终的效果是.onclick=fn2 执行</p>
</blockquote>
<ul>
<li>2.3 addEventListener方式(了解)</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> i0 = <span class="built_in">document</span>.getElementById(<span class="string">'i1'</span>);</span><br><span class="line">i0.addEventListener(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="built_in">console</span>.log(<span class="string">'a'</span>);&#125;)</span><br><span class="line">i0.addEventListener(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="built_in">console</span>.log(<span class="string">'b'</span>);&#125;)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>格式：addEventListener(‘事件’,绑定的函数, true/false);</p>
<p>1.如果添加了多个事件处理函数，则按添加时的先后顺序来执行<br>2.事件处理函数中的this指代的是DOM对象本身(w3c标准)<br>3.第一个事件参数，一律把事件名称的“on”去掉</p>
<p>优点: 能为同一种事件,如onclick,绑定多个函数函数中的this指向该DOM对象,操作方便</p>
<p>缺点:IE8及以下版本不支持此方法</p>
</blockquote>
<hr>
<h3 id="五、window对象"><a href="#五、window对象" class="headerlink" title="五、window对象"></a><div id="title">五、window对象</div></h3><h4 id="1-window对象常见的方法"><a href="#1-window对象常见的方法" class="headerlink" title="1.window对象常见的方法"></a>1.window对象常见的方法</h4><p><strong>1.1 随手就能用到的方法</strong></p>
<blockquote>
<ol>
<li>window.alert(msg);</li>
<li>window.confirm(msg);</li>
<li>window.open(URL, 位置);</li>
</ol>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.alert(<span class="string">'fuck'</span>);</span><br><span class="line"><span class="built_in">window</span>.confirm(<span class="string">'确认要删除吗？'</span>);</span><br><span class="line"><span class="built_in">window</span>.open(<span class="string">'./02.html'</span>, <span class="string">'_blank'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 02.html如下代码：</span></span><br><span class="line">&lt;input type=<span class="string">'button'</span> onclick=<span class="string">'window.close()'</span> /&gt;</span><br></pre></td></tr></table></figure>

<p><strong>1.2 比较常用到的方法</strong></p>
<blockquote>
<p>setInerval()</p>
<blockquote>
<ul>
<li>在载入后，每隔指定的时间就执行一次回调函数</li>
<li>timer = setInterval(“函数()” , 毫秒数);</li>
</ul>
</blockquote>
</blockquote>
<blockquote>
<p>clearInterval()</p>
<blockquote>
<ul>
<li>取消setInterval()的设置</li>
<li>clearInterval(timer)</li>
</ul>
</blockquote>
</blockquote>
<blockquote>
<p>setTimeout()</p>
<blockquote>
<ul>
<li>在载入后，在指定的时间就执行一次回调函数（<font color="red">只是一次</font>）</li>
<li>timer = setTimeout(“函数()” , 毫秒数);</li>
</ul>
</blockquote>
</blockquote>
<blockquote>
<p>clearTimeout()</p>
<blockquote>
<ul>
<li>取消setTimeout()的设置</li>
<li>clearTimeout(timer)</li>
</ul>
</blockquote>
</blockquote>
<h4 id="2-window对象常见的属性"><a href="#2-window对象常见的属性" class="headerlink" title="2.window对象常见的属性"></a>2.window对象常见的属性</h4><blockquote>
<p>window.location</p>
</blockquote>
<p><strong>利用window对象的子对象location的href属性，可以完成简单的页面跳转</strong></p>
<blockquote>
<ol>
<li>window.location.href　　　　　　获取URL</li>
<li>window.location.href = ‘url’    重定向</li>
<li>window.location.reload　　　　　重新加载</li>
</ol>
</blockquote>
<hr>
<h3 id="六、常见的案例"><a href="#六、常见的案例" class="headerlink" title="六、常见的案例"></a><div id="title">六、常见的案例</div></h3><p><strong>案例：表格的全选和反选以及取消</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">&lt;input type=<span class="string">"button"</span> value=<span class="string">"全选"</span> onclick=<span class="string">"SelectAll();"</span>/&gt;</span><br><span class="line">&lt;input type=<span class="string">"button"</span> value=<span class="string">"取消"</span> onclick=<span class="string">"CancelAll();"</span>/&gt;</span><br><span class="line">&lt;input type=<span class="string">"button"</span> value=<span class="string">"反选"</span> onclick=<span class="string">"ReverseAll();"</span>/&gt;</span><br><span class="line"></span><br><span class="line">&lt;table border=<span class="string">"1"</span>&gt;</span><br><span class="line">    &lt;thead&gt;</span><br><span class="line">        &lt;tr&gt;</span><br><span class="line">            &lt;th&gt;选择&lt;<span class="regexp">/th&gt;</span></span><br><span class="line"><span class="regexp">            &lt;th&gt;ip&lt;/</span>th&gt;</span><br><span class="line">            &lt;th&gt;端口&lt;<span class="regexp">/th&gt;</span></span><br><span class="line"><span class="regexp">        &lt;/</span>tr&gt;</span><br><span class="line">    &lt;<span class="regexp">/thead&gt;</span></span><br><span class="line"><span class="regexp">    &lt;tbody id="infobody"&gt;</span></span><br><span class="line"><span class="regexp">        &lt;tr&gt;</span></span><br><span class="line"><span class="regexp">            &lt;td&gt;&lt;input type="checkbox" /</span>&gt;&lt;/td&gt;</span><br><span class="line">            &lt;td&gt;<span class="number">192.16</span><span class="number">.0</span><span class="number">.100</span>&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">            &lt;td&gt;80&lt;/</span>td&gt;</span><br><span class="line">        &lt;<span class="regexp">/tr&gt;</span></span><br><span class="line"><span class="regexp">        &lt;tr&gt;</span></span><br><span class="line"><span class="regexp">            &lt;td&gt;&lt;input type="checkbox" /</span>&gt;&lt;/td&gt;</span><br><span class="line">            &lt;td&gt;<span class="number">192.16</span><span class="number">.0</span><span class="number">.100</span>&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">            &lt;td&gt;81&lt;/</span>td&gt;</span><br><span class="line">        &lt;<span class="regexp">/tr&gt;</span></span><br><span class="line"><span class="regexp">        &lt;tr&gt;</span></span><br><span class="line"><span class="regexp">            &lt;td&gt;&lt;input type="checkbox" /</span>&gt;&lt;/td&gt;</span><br><span class="line">            &lt;td&gt;<span class="number">192.16</span><span class="number">.0</span><span class="number">.100</span>&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">            &lt;td&gt;82&lt;/</span>td&gt;</span><br><span class="line">        &lt;<span class="regexp">/tr&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>tbody&gt;</span><br><span class="line">&lt;<span class="regexp">/table&gt;</span></span><br><span class="line"><span class="regexp">    &lt;script&gt;</span></span><br><span class="line"><span class="regexp">        function SelectAll()&#123;</span></span><br><span class="line"><span class="regexp">            var InfoBody = document.getElementById('infobody');</span></span><br><span class="line"><span class="regexp">            var InfoChilden = InfoBody.children;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">            for(var i = 0; i &lt; InfoChilden.length; i++)&#123;</span></span><br><span class="line"><span class="regexp">                var myInput = InfoChilden[i].children[0].children[0];</span></span><br><span class="line"><span class="regexp">                myInput.checked = true;</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        &#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        function CancelAll()&#123;</span></span><br><span class="line"><span class="regexp">            var InfoBody = document.getElementById('infobody');</span></span><br><span class="line"><span class="regexp">            var InfoChilden = InfoBody.children;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">            for(var i = 0; i &lt; InfoChilden.length; i++)&#123;</span></span><br><span class="line"><span class="regexp">                var myInput = InfoChilden[i].children[0].children[0];</span></span><br><span class="line"><span class="regexp">                myInput.checked = false;</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        &#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        function ReverseAll()&#123;</span></span><br><span class="line"><span class="regexp">            var InfoBody = document.getElementById('infobody');</span></span><br><span class="line"><span class="regexp">            var InfoChilden = InfoBody.children;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">            for(var i = 0; i &lt; InfoChilden.length; i++)&#123;</span></span><br><span class="line"><span class="regexp">                var myInput = InfoChilden[i].children[0].children[0];</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">                if(myInput.checked)&#123;</span></span><br><span class="line"><span class="regexp">                    myInput.checked = false;</span></span><br><span class="line"><span class="regexp">                &#125;else&#123;</span></span><br><span class="line"><span class="regexp">                    myInput.checked = true;</span></span><br><span class="line"><span class="regexp">                &#125;</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        &#125;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>script&gt;</span><br></pre></td></tr></table></figure>

<p><strong>案例：左侧菜单展示</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">&lt;style&gt;</span><br><span class="line">        .header&#123;</span><br><span class="line">            width:<span class="number">400</span>px;</span><br><span class="line">            height:<span class="number">38</span>px;</span><br><span class="line">            line-height: <span class="number">38</span>px;</span><br><span class="line">            background-color: #204982;</span><br><span class="line">            cursor: pointer;</span><br><span class="line">        &#125;</span><br><span class="line">        .hide&#123;</span><br><span class="line">            display: none;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;<span class="regexp">/style&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"navigator"</span>&gt;</span><br><span class="line"></span><br><span class="line">        &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"item"</span>&gt;</span><br><span class="line">            &lt;div id=<span class="string">"i1"</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">"header"</span> onclick=<span class="string">"changeMenu('i1')"</span>&gt;菜单一&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">            &lt;div class="content hide"&gt;</span></span><br><span class="line"><span class="regexp">                &lt;div&gt;111111&lt;/</span>div&gt;</span><br><span class="line">                &lt;div&gt;<span class="number">111111</span>&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">                &lt;div&gt;111111&lt;/</span>div&gt;</span><br><span class="line">            &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">        &lt;/</span>div&gt;</span><br><span class="line"></span><br><span class="line">        &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"item"</span>&gt;</span><br><span class="line">            &lt;div  id=<span class="string">"i2"</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">"header"</span> onclick=<span class="string">"changeMenu('i2')"</span>&gt;菜单二&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">            &lt;div class="content hide"&gt;</span></span><br><span class="line"><span class="regexp">                &lt;div&gt;222222&lt;/</span>div&gt;</span><br><span class="line">                &lt;div&gt;<span class="number">222222</span>&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">                &lt;div&gt;222222&lt;/</span>div&gt;</span><br><span class="line">            &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">        &lt;/</span>div&gt;</span><br><span class="line"></span><br><span class="line">        &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"item"</span>&gt;</span><br><span class="line">            &lt;div  id=<span class="string">"i3"</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">"header"</span> onclick=<span class="string">"changeMenu('i3')"</span>&gt;菜单三&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">            &lt;div class="content hide"&gt;</span></span><br><span class="line"><span class="regexp">                &lt;div&gt;333333&lt;/</span>div&gt;</span><br><span class="line">                &lt;div&gt;<span class="number">333333</span>&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">                &lt;div&gt;333333&lt;/</span>div&gt;</span><br><span class="line">            &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">        &lt;/</span>div&gt;</span><br><span class="line"></span><br><span class="line">    &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">    &lt;script&gt;</span></span><br><span class="line"><span class="regexp">        function changeMenu(nid)&#123;</span></span><br><span class="line"><span class="regexp">            /</span><span class="regexp">/ 1. 先获取到当前点击的div</span></span><br><span class="line"><span class="regexp">            var current_div = document.getElementById(nid);</span></span><br><span class="line"><span class="regexp">/</span><span class="regexp">/            console.log(current_div);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">            /</span><span class="regexp">/ 2.通过当前点击的div，从而获取到其父节点 class=navigator</span></span><br><span class="line"><span class="regexp">            var elements = current_div.parentElement.parentElement.children;</span></span><br><span class="line"><span class="regexp">/</span><span class="regexp">/            console.log(elements);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">            /</span><span class="regexp">/ 3.通过获取到的父级元素，然后设置content元素为hide</span></span><br><span class="line"><span class="regexp">            for(var i=0; i&lt;elements.length; i++)&#123;</span></span><br><span class="line"><span class="regexp">                elements[i].children[1].classList.add('hide');</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">            /</span><span class="regexp">/ 4.将点击的那个div的hide样式去除掉</span></span><br><span class="line"><span class="regexp">            current_div.nextElementSibling.classList.remove('hide');</span></span><br><span class="line"><span class="regexp">        &#125;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>script&gt;</span><br></pre></td></tr></table></figure>

<p><strong>案例：模态框的显示和取消</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line">&lt;style&gt;</span><br><span class="line">        .one&#123;</span><br><span class="line">            position: fixed;</span><br><span class="line">            top:<span class="number">0</span>;</span><br><span class="line">            left:<span class="number">0</span>;</span><br><span class="line">            right:<span class="number">0</span>;</span><br><span class="line">            bottom:<span class="number">0</span>;</span><br><span class="line">            background-color: black;</span><br><span class="line">            opacity: <span class="number">0.6</span>;</span><br><span class="line">            z-index: <span class="number">9</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        .two&#123;</span><br><span class="line">            position: fixed;</span><br><span class="line">            width: <span class="number">500</span>px;</span><br><span class="line">            height:<span class="number">400</span>px;</span><br><span class="line">            <span class="comment">/*top:200px;*/</span></span><br><span class="line">            left:<span class="number">40</span>%;</span><br><span class="line">            background-color: white;</span><br><span class="line">            z-index: <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        .hide&#123;</span><br><span class="line">            display: none;</span><br><span class="line">        &#125;</span><br><span class="line">        .show&#123;</span><br><span class="line">            display: block;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;<span class="regexp">/style&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;input type=<span class="string">"button"</span> value=<span class="string">"添加"</span> onclick=<span class="string">"showModel();"</span>/&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- 遮罩层开始 --&gt;</span><br><span class="line">    &lt;div id=<span class="string">'one'</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">"one hide"</span>&gt;&lt;/div&gt;</span><br><span class="line">    &lt;!-- 遮罩层结束 --&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- 模态弹出框开始 --&gt;</span><br><span class="line">    &lt;div id=<span class="string">'two'</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">"two hide"</span>&gt;</span><br><span class="line"></span><br><span class="line">        &lt;p&gt;&lt;input type="text" value=""/&gt;&lt;/p&gt;</span><br><span class="line">        &lt;p&gt;&lt;input type="text" value=""/&gt;&lt;/p&gt;</span><br><span class="line">        &lt;p&gt;&lt;input type="button" value="取消" onclick="hideModel();"/&gt;</span><br><span class="line">        &lt;input type="button" value="确定" /&gt;&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &lt;!-- 模态弹出框结束 --&gt;</span><br><span class="line"></span><br><span class="line">    &lt;script&gt;</span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">showModel</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">var</span> myonediv = <span class="built_in">document</span>.getElementById(<span class="string">'one'</span>);</span><br><span class="line">            <span class="keyword">var</span> mytwodiv = <span class="built_in">document</span>.getElementById(<span class="string">'two'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            myonediv.classList.remove(<span class="string">'hide'</span>);</span><br><span class="line">            mytwodiv.classList.remove(<span class="string">'hide'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">hideModel</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">var</span> myonediv = <span class="built_in">document</span>.getElementById(<span class="string">'one'</span>);</span><br><span class="line">            <span class="keyword">var</span> mytwodiv = <span class="built_in">document</span>.getElementById(<span class="string">'two'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            myonediv.classList.add(<span class="string">'hide'</span>);</span><br><span class="line">            mytwodiv.classList.add(<span class="string">'hide'</span>);</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>案例：发送验证码倒计时</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;button id=<span class="string">"btn"</span> &gt;发送验证码&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">var</span> total = <span class="number">10</span>; <span class="comment">// 总共多少秒</span></span><br><span class="line">    <span class="keyword">var</span> curCount; <span class="comment">// 当前剩余秒数</span></span><br><span class="line">    <span class="keyword">var</span> mytimer;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> btn = <span class="built_in">document</span>.getElementById(<span class="string">"btn"</span>);</span><br><span class="line"></span><br><span class="line">    btn.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        curCount = total;</span><br><span class="line">        <span class="keyword">this</span>.setAttribute(<span class="string">"disabled"</span>,<span class="string">"true"</span>);</span><br><span class="line">        <span class="keyword">this</span>.innerText = <span class="string">"请在"</span> + curCount + <span class="string">"秒内输入验证码"</span>;</span><br><span class="line">        mytimer = setInterval(setRemainTime, <span class="number">1000</span>);</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">setRemainTime</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(curCount == <span class="number">0</span>)&#123;</span><br><span class="line">            clearInterval(mytimer);</span><br><span class="line">            btn.removeAttribute(<span class="string">"disabled"</span>);</span><br><span class="line">            btn.innerText = <span class="string">"重新发送验证码"</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            curCount -= <span class="number">1</span>;</span><br><span class="line">            btn.innerText = <span class="string">"请在"</span> + curCount + <span class="string">"秒内输入验证码"</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>案例：表格的隔行换色</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">&lt;table border=<span class="string">"1"</span> width=<span class="string">"500px"</span>&gt;</span><br><span class="line">    &lt;tr onmouseover=<span class="string">"test1(0);"</span> onmouseout=<span class="string">"test2(0);"</span>&gt;</span><br><span class="line">        &lt;td&gt;&amp;nbsp&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">        &lt;td&gt;&amp;nbsp&lt;/</span>td&gt;</span><br><span class="line">        &lt;td&gt;&amp;nbsp&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>tr&gt;</span><br><span class="line">    &lt;tr onmouseover=<span class="string">"test1(1);"</span> onmouseout=<span class="string">"test2(1);"</span>&gt;</span><br><span class="line">        &lt;td&gt;&amp;nbsp&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">        &lt;td&gt;&amp;nbsp&lt;/</span>td&gt;</span><br><span class="line">        &lt;td&gt;&amp;nbsp&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>tr&gt;</span><br><span class="line">    &lt;tr onmouseover=<span class="string">"test1(2);"</span> onmouseout=<span class="string">"test2(2);"</span>&gt;</span><br><span class="line">        &lt;td&gt;&amp;nbsp&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">        &lt;td&gt;&amp;nbsp&lt;/</span>td&gt;</span><br><span class="line">        &lt;td&gt;&amp;nbsp&lt;<span class="regexp">/td&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>tr&gt;</span><br><span class="line">&lt;<span class="regexp">/table&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">test1</span>(<span class="params">n</span>)</span>&#123;</span><br><span class="line"><span class="comment">//        console.log(123);</span></span><br><span class="line">        <span class="keyword">var</span> myTr = <span class="built_in">document</span>.getElementsByTagName(<span class="string">"tr"</span>)[n];</span><br><span class="line">        myTr.style.backgroundColor = <span class="string">"red"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">test2</span>(<span class="params">n</span>)</span>&#123;</span><br><span class="line"><span class="comment">//        console.log(456);</span></span><br><span class="line">        <span class="keyword">var</span> myTr = <span class="built_in">document</span>.getElementsByTagName(<span class="string">"tr"</span>)[n];</span><br><span class="line">        myTr.style.backgroundColor = <span class="string">""</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>案例：文本框获取和失去交点</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;input onfocus=<span class="string">"Focus(this);"</span> onblur=<span class="string">"Blur(this);"</span> id=<span class="string">"search"</span> value=<span class="string">"请输入关键字"</span> style=<span class="string">"color: gray;"</span> /&gt;</span><br><span class="line"></span><br><span class="line">    &lt;script&gt;</span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">Focus</span>(<span class="params">ths</span>)</span>&#123;</span><br><span class="line">            ths.style.color = <span class="string">"black"</span>;</span><br><span class="line">            <span class="keyword">if</span>(ths.value == <span class="string">'请输入关键字'</span> || ths.value.trim() == <span class="string">""</span>)&#123;</span><br><span class="line"></span><br><span class="line">                ths.value = <span class="string">""</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">Blur</span>(<span class="params">ths</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(ths.value.trim() == <span class="string">""</span>)&#123;</span><br><span class="line">                ths.value = <span class="string">'请输入关键字'</span>;</span><br><span class="line">                ths.style.color = <span class="string">'gray'</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                ths.style.color = <span class="string">"black"</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>案例：菜单联动</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">&lt;select name=<span class="string">"province"</span> id=<span class="string">"province"</span> onchange=<span class="string">"ld();"</span>&gt;</span><br><span class="line">    &lt;option value=<span class="string">"-1"</span>&gt;请选择&lt;<span class="regexp">/option&gt;</span></span><br><span class="line"><span class="regexp">    &lt;option value="0"&gt;北京&lt;/</span>option&gt;</span><br><span class="line">    &lt;option value=<span class="string">"1"</span>&gt;山西&lt;<span class="regexp">/option&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>select&gt;</span><br><span class="line">&lt;select name=<span class="string">"city"</span> id=<span class="string">"city"</span>&gt;</span><br><span class="line"></span><br><span class="line">&lt;<span class="regexp">/select&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">var</span> citys = [</span><br><span class="line">        [<span class="string">"海淀"</span>,<span class="string">"东城"</span>,<span class="string">"西城"</span>,<span class="string">"朝阳"</span>],</span><br><span class="line">        [<span class="string">"太原"</span>,<span class="string">"大同"</span>,<span class="string">"运城"</span>,<span class="string">"晋中"</span>]</span><br><span class="line">    ];</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">ld</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> provinces = <span class="built_in">document</span>.getElementById(<span class="string">"province"</span>);</span><br><span class="line">        <span class="keyword">var</span> opts;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(provinces.value == <span class="number">-1</span>)&#123;</span><br><span class="line">            opts = <span class="string">''</span>;</span><br><span class="line">            <span class="built_in">document</span>.getElementById(<span class="string">"city"</span>).innerHTML = opts;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">var</span> areas = citys[provinces.value];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; areas.length; i++)&#123;</span><br><span class="line">            opts += <span class="string">"&lt;option value='"</span> + i + <span class="string">"'&gt;"</span> + areas[i] + <span class="string">"&lt;/option&gt;"</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">document</span>.getElementById(<span class="string">"city"</span>).innerHTML = opts;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>案例：小说阅读，点击大中小更改字体</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;button onclick=<span class="string">"bigger();"</span>&gt;大&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">    &lt;button onclick="middle();"&gt;中&lt;/</span>button&gt;</span><br><span class="line">    &lt;button onclick=<span class="string">"smaller();"</span>&gt;小&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">&lt;p&gt;轻轻地我走了，正如我轻轻的来！&lt;/</span>p&gt;</span><br><span class="line">&lt;<span class="regexp">/body&gt;</span></span><br><span class="line"><span class="regexp">&lt;script&gt;</span></span><br><span class="line"><span class="regexp">    function bigger()&#123;</span></span><br><span class="line"><span class="regexp">        var p = document.getElementsByTagName("p")[0];</span></span><br><span class="line"><span class="regexp">        p.style.fontSize = '25px';</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">    function middle()&#123;</span></span><br><span class="line"><span class="regexp">        var p = document.getElementsByTagName("p")[0];</span></span><br><span class="line"><span class="regexp">        p.style.fontSize = '18px';</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">    function smaller()&#123;</span></span><br><span class="line"><span class="regexp">        var p = document.getElementsByTagName("p")[0];</span></span><br><span class="line"><span class="regexp">        p.style.fontSize = '13px';</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp">&lt;/</span>script&gt;</span><br></pre></td></tr></table></figure>
    </div>

  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM操作"><span class="toc-text">DOM操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#一、前戏"><span class="toc-text">一、前戏</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二、DOM对象–查找元素"><span class="toc-text">二、DOM对象–查找元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三、DOM对象–属性操作"><span class="toc-text">三、DOM对象–属性操作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#四、事件"><span class="toc-text">四、事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#五、window对象"><span class="toc-text">五、window对象</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-window对象常见的方法"><span class="toc-text">1.window对象常见的方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-window对象常见的属性"><span class="toc-text">2.window对象常见的属性</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#六、常见的案例"><span class="toc-text">六、常见的案例</span></a></li></ol></li></ol>
  </div>


  </div>
</div>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=5sr5du46f27&amp;m=0&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33" async="async"></script>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>


  
    <div class="post-nav" style="margin-left:-168px;">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2017/05/07/CSS基础学习/" rel="next" title="第十五章 CSS基础">
          第十五章 CSS基础
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2017/12/09/房价的本质/" rel="prev" title="房价的本质">
            房价的本质
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


	
	<div style="width:109%; margin-left:-144px" id="lv-container" data-id="city" data-uid="MTAyMC80OTg5NS8yNjM4Ng==">
	<script type="text/javascript">
   	   (function(d, s) {
       		var j, e = d.getElementsByTagName(s)[0];

       		if (typeof LivereTower === 'function') { return; }

       		j = d.createElement(s);
       		j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       		j.async = true;

       		e.parentNode.insertBefore(j, e);
   	   })(document, 'script');
	</script>
	<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
        </div>
	



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href target="_blank">GitHub</a> |
        <a class="bottom-item" href>友情链接</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/fooying/hexo-theme-xoxo-plus" target="_blank">Theme xoxo-plus</a> |
        <a class="bottom-item" href="/atom.xml">订阅</a>
    </div>
</footer>

  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



  

</body>
</html>
